﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Configuration;

namespace SharpShare.Management.Mmc {
    public class ConfigurationElementTypeDescriptor : CustomTypeDescriptor {
        public ConfigurationElementTypeDescriptor(ICustomTypeDescriptor parentDescriptor)
            : base(parentDescriptor) {
        }

        public override PropertyDescriptorCollection GetProperties() {
            return this.GetProperties(null);
        }
        public override PropertyDescriptorCollection GetProperties(Attribute[] attributes) {
            PropertyDescriptorCollection allProperties;

            if (attributes == null) {
                allProperties = base.GetProperties();
            } else {
                allProperties = base.GetProperties(attributes);
            }

            PropertyDescriptorCollection properties = new PropertyDescriptorCollection(
                allProperties
                    .Cast<PropertyDescriptor>()
                    .Select(d=> {
                        if (d.ComponentType == typeof(ConfigurationElement) || d.ComponentType == typeof(ConfigurationSection)) {
                            return TypeDescriptor.CreateProperty(d.ComponentType, d, new BrowsableAttribute(false));
                        }

                        return d;
                    })
                    .ToArray());

            return properties;
        }
    }
}
